package com.whpost.service.service.t;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whpost.service.dao.t.TMenuDao;
import com.whpost.service.dao.t.TPermissionDao;
import com.whpost.service.dao.t.TRoleDao;
import com.whpost.service.entity.t.TCzy;
import com.whpost.service.entity.t.TMenu;
import com.whpost.service.entity.t.TPermission;
import com.whpost.service.service.BaseServices;
import com.whpost.service.tools.ToolBean;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: Yin Zhuoyi
 * @Date: 2021/12/15 11:16
 */
@Service
public class TPermissionServices extends BaseServices {

    @Resource
    private TPermissionDao tPermissionDao;
    @Resource
    private TRoleDao tRoleDao;
    @Resource
    private TMenuDao tMenuDao;
    public List<String> getPercodes(String loginId){
        List<String> rtnList = new ArrayList<>();;   //可以分配的权限列表
        TCzy tCzy = new TCzy();
        tCzy = gettCzyDao().selectById(loginId);
        if ("1".equals(loginId)){
            rtnList = tPermissionDao.getPerCode(new QueryWrapper<TPermission>().eq("state","1"));
        }else{
            String pers="";
            pers = tRoleDao.selectById(tCzy.getRoleId()).getPermissionId();
            List<String> perlist = Arrays.asList(pers.split(","));
            //ToolBean.info("获取到的pers:"+pers);
            rtnList = tPermissionDao.getPerCode(new QueryWrapper<TPermission>().eq("state","1").in("id",perlist));
        }
        return rtnList;
    }

    public String getPermissionCode(String id){
        return tPermissionDao.selectById(id).getCode();
    }

    public boolean addPermission(TMenu tmenu) {

        try {

            Map<String, String> pMap = new HashMap<>();

            pMap.put("add", "增加");
            pMap.put("del", "删除");
            pMap.put("update", "修改");
            pMap.put("query", "查询");
            TPermission tPermission = new TPermission();
            Long sl = tPermissionDao.selectCount(new QueryWrapper<TPermission>().eq("menu_id", tmenu.getId()));
            if (sl > 0) {
                //修改权限代码
                List<TPermission> tPermissionList = new ArrayList<>();
                tPermissionList = tPermissionDao.selectList(new QueryWrapper<TPermission>().eq("menu_id", tmenu.getId()));
                for (int i=0;i<tPermissionList.size();i++){
                    tPermission = new TPermission();
                    tPermission =tPermissionList.get(i);
                    String code = tPermission.getCode();
                    if (code.indexOf("add")>=0) code=tmenu.getQxdm()+":add";
                    else if (code.indexOf("update")>=0) code=tmenu.getQxdm()+":update";
                    else if (code.indexOf("del")>=0) code=tmenu.getQxdm()+":del";
                    else if (code.indexOf("update")>=0) code=tmenu.getQxdm()+":update";
                    tPermission.setCode(code);
                    tPermission.setZjm(code);
                    tPermissionDao.updateById(tPermission);
                }
                return true;
            } else {
                //新增
                String bz = tmenu.getQxdm();
                for (Map.Entry<String, String> entry : pMap.entrySet()) {
                    // 处理元素
                    tPermission = new TPermission();
                    tPermission.setMenuId(tmenu.getId());
                    tPermission.setName(tmenu.getName() + entry.getValue());
                    tPermission.setZjm(bz + ":" + entry.getKey());
                    tPermission.setCode(bz + ":" + entry.getKey());
                    tPermissionDao.insert(tPermission);
                }
                return true;
            }
        } catch (Exception e) {
            ToolBean.info("addPermission发生错误：" + e.toString());
            return false;
        }
    }
}
